﻿#include "rongqing_oil_leakage.h"

// 使用 utf8 编码
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

// 融青漏油检测

/// <summary>
/// 
/// </summary>
rongqing_oil_leakage::rongqing_oil_leakage()
{
	m_pMotorDet = nullptr;
	m_pMotorRectDefect = nullptr;
	m_pMotorRectClass = nullptr;

	m_pFloorDet = nullptr;

	// 记录 M8 型号的站点
	m_M8_ID_List.push_back("670006");
	m_M8_ID_List.push_back("670013");
	m_M8_ID_List.push_back("670014");
	m_M8_ID_List.push_back("670015");
	m_M8_ID_List.push_back("670016");
	m_M8_ID_List.push_back("670017");
	m_M8_ID_List.push_back("670018");
	m_M8_ID_List.push_back("670020");
	m_M8_ID_List.push_back("670023");



}


/// <summary>
/// 
/// </summary>
rongqing_oil_leakage::~rongqing_oil_leakage()
{

	if (m_pMotorDet != nullptr)
	{
		m_pMotorDet->Release();
		m_pMotorDet = nullptr;
	}

	if (m_pMotorRectDefect != nullptr)
	{
		m_pMotorRectDefect->Release();
		m_pMotorRectDefect = nullptr;
	}
	if (m_pMotorRectClass != nullptr)
	{
		m_pMotorRectClass->Release();
		m_pMotorRectClass = nullptr;
	}

	if (m_pFloorDet != nullptr)
	{
		m_pFloorDet->Release();
		m_pFloorDet = nullptr;
	}
}


/// <summary>
/// 
/// </summary>
/// <returns></returns>
void rongqing_oil_leakage::init()
{
	m_pMotorDet = CreateDetObj(m_strRQ_MotorDetModelPath);
	m_pMotorRectDefect = CreateDetObj(m_strRQ_MotorRectDefectModelPath);
	m_pMotorRectClass = CreateClsObj(m_strRQ_MotorRectClassModelPath);

	m_pFloorDet = CreateDetObj(m_strRQ_FloorDetModelPath);
	if (m_pFloorDet != nullptr)
	{
		m_pFloorDet->setScore(0.6);
	}

}


/// <summary>
/// 
/// </summary>
/// <param name="pData"></param>
void rongqing_oil_leakage::execute(QSharedPointer<IMG_DATA> pData, int nBuffersize)
{
	IMG_DATA data = *pData;
	data.image = urlToPixmap(data.strUrl);
	saveImage(data);

	cv::Mat image = QImage2cvMat(data.image);
	int nDispId = data.nDispId;

	if (!image.data || m_pMotorDet == nullptr)
	{
		return;
	}

	std::vector<DetectResult> results_Motor;
	std::vector<DetectResult> results_Floor;


	// 如果是广西百色德保中石化2号换电站 （电机）
	if (m_M8_ID_List.contains(data.strStationId))
		//if (data.strStationId == "670014")
	{

		m_pMotorDet->Evaluate(image, results_Motor);

		// 保存图片
		saveImage(image, "融青电机(定位)", data, results_Motor.size());


		for (int i = 0; i < results_Motor.size(); i++)
		{
			DetectResult res = results_Motor[i];
			cv::Rect rect = res.box;
			rect &= cv::Rect(0, 0, image.cols, image.rows);//求交集
			cv::Mat image_roi = image(rect);




			// 检测
			std::vector<DetectResult> def_results;
			if (m_pMotorRectDefect != nullptr)
			{
				m_pMotorRectDefect->Evaluate(image_roi, def_results);
			}

			// 如果没检测到破损，则不发布破损相关的数据

			for (int i = 0; i < def_results.size(); i++)
			{
				DetectResult res = def_results[i];
				cv::Rect rect = res.box;
				rect &= cv::Rect(0, 0, image_roi.cols, image_roi.rows);//求交集
				cv::Mat roi = image_roi(rect);


			
				std::vector<int> rstClassList;
				std::vector<float> rstProbList;

				std::vector<cv::Mat> srcImgs;
				srcImgs.push_back(roi);

				if (m_pMotorRectClass != nullptr)
				{
					m_pMotorRectClass->Evaluate(srcImgs, rstClassList, rstProbList);
				}

				// 0 表示 OK 类别，1 表示 NG 类别
				if (rstClassList[0] == 1)
				{
					// 返回结果到 kafka 
					_tagResults tagResults;
					tagResults.setImageData(data);
					tagResults.setResults("漏油", def_results, res.box.x, res.box.y);
					tagResults.setVersion(m_strRQ_MotorRectDefectModelPath);

					QString strResultTopc = data.strTopicName + suffix_2;
					QString strResultData = tagResults.toJSon();

					pushKafka(strResultTopc, strResultData, data.strStationId);

				}

				saveImage(roi, "融青漏油(裁剪)", data, rstClassList[0]);


			}




			QPainter painter(&data.image);

			if (def_results.size() == 0)
			{
				painter.setBrush(QBrush(QColor(0, 255, 0, 50)));
				QPen pen(QColor(0, 255, 0));
				pen.setWidth(5);
				painter.setPen(pen);
			}
			else
			{
				painter.setBrush(QBrush(QColor(255, 0, 0, 50)));
				QPen pen(Qt::red);
				pen.setWidth(5);
				painter.setPen(pen);
			}

			saveImage(image_roi, "融青电机漏油", data, def_results.size());

			cv::Point point = getCenterPoint(rect);
			painter.drawRect(rect.tl().x, rect.tl().y, rect.width, rect.height);

			emit updateShow(data.nDispId, data.image, nBuffersize);
		}
	}
	// 地板
	else
	{
		m_pFloorDet->Evaluate(image, results_Floor);

		// 保存图片
		saveImage(image, "融青地板(定位)", data, results_Floor.size());

		for (int i = 0; i < results_Floor.size(); i++)
		{
			DetectResult res = results_Floor[i];
			cv::Rect rect = res.box;
			rect &= cv::Rect(0, 0, image.cols, image.rows);//求交集
			cv::Mat image_roi = image(rect);

			std::vector<DetectResult> def_results;
			// 			if (m_pRectDefect != nullptr)
			// 			{
			// 				m_pRectDefect->Evaluate(image_roi, def_results);
			// 			}

						// 如果没检测到破损，则不发布破损相关的数据
			if (def_results.size() != 0)
			{
				// 返回结果到 kafka 
				_tagResults tagResults;
				tagResults.setImageData(data);
				tagResults.setResults("漏油", def_results, res.box.x, res.box.y);
				tagResults.setVersion("");

				QString strResultTopc = data.strTopicName + suffix_2;
				QString strResultData = tagResults.toJSon();

				pushKafka(strResultTopc, strResultData, data.strStationId);
			}


			QPainter painter(&data.image);
			painter.setBrush(QBrush(QColor(255, 0, 0, 50)));
			QPen pen(Qt::red);
			pen.setWidth(5);
			painter.setPen(pen);

			saveImage(image_roi, "融青地板漏油", data, -1);

			cv::Point point = getCenterPoint(rect);
			painter.drawRect(rect.tl().x, rect.tl().y, rect.width, rect.height);

			emit updateShow(data.nDispId, data.image, nBuffersize);
		}

	}




	// 返回结果到 kafka 
	_tagResults tagResults;
	tagResults.setImageData(data);
	tagResults.setResults("马达", results_Motor);
	tagResults.setResults("地板", results_Floor);

	tagResults.setVersion(m_strRQ_FloorDetModelPath);

	QString strResultTopc = data.strTopicName + suffix_1;
	QString strResultData = tagResults.toJSon();

	pushKafka(strResultTopc, strResultData, data.strStationId);
}
